package com.v1.video.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.v1.video.R;
import com.v1.video.domain.Channel;
import com.v1.video.util.Utils;
/**
 * 频道管理适配器
 * @author wangfang
 *
 */
public class DragAdapter extends BaseAdapter {

	public interface OnUpdateListener {
		public void onUpdateListener();
	}

	/** TAG */
	private final static String TAG = "DragAdapter";
	/** 是否显示底部的ITEM */
	private boolean isItemShow = false;
	private Context context;
	/** 控制的postion */
	private int holdPosition;
	/** 是否改变 */
	private boolean isChanged = false;
	/** 是否可见 */
	boolean isVisible = true;
	/** 可以拖动的列表（即用户选择的频道列表） */
	public List<Channel> channelList;
	/** TextView 频道内容 */
	private TextView item_text;
	private ImageView item_image;
	/** 要删除的position */
	public int remove_position = -1;

	private LayoutInflater mLayoutInflater;

	private OnUpdateListener mOnUpdateListener;

	private boolean isEdit = false;

	private boolean isSort = false;

	private int mIndex = 0;

	public DragAdapter(Context context, List<Channel> channelList) {
		this.context = context;
		this.channelList = channelList;
		this.mLayoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return channelList == null ? 0 : channelList.size();
	}

	@Override
	public Channel getItem(int position) {
		// TODO Auto-generated method stub
		if (channelList != null && channelList.size() != 0) {
			return channelList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = mLayoutInflater.inflate(
				R.layout.activity_channelmanager_item, null);
		item_text = (TextView) convertView.findViewById(R.id.tv_item);
		item_image = (ImageView) convertView.findViewById(R.id.iv_image);
		Channel channel = getItem(position);
		item_text.setText(channel.getName());
		if (isEdit == true) {
			if (position <= 1) {
				item_image.setVisibility(View.GONE);
				item_text.setTextColor(context.getResources().getColor(
						R.color.channel_color_text_enable));
				item_text.setEnabled(false);
			} else {
				item_image.setVisibility(View.VISIBLE);
				item_text.setTextColor(context.getResources().getColor(
						R.color.channel_color_text_default));
				item_text.setEnabled(true);
			}
		} else {
			item_image.setVisibility(View.GONE);
			item_text.setTextColor(context.getResources().getColor(
					R.color.channel_color_text_default));
			item_text.setEnabled(true);
			if (position == mIndex) {
				item_text.setTextColor(context.getResources().getColor(
						R.color.channel_color_text_selected));
			}
		}
		if (isChanged && (position == holdPosition) && !isItemShow) {
			item_text.setText("");
			item_text.setSelected(true);
			item_text.setEnabled(true);
			isChanged = false;
		}
		if (!isVisible && (position == -1 + channelList.size())) {
			item_text.setText("");
			item_text.setSelected(true);
			item_text.setEnabled(true);
		}
		if (remove_position == position) {
			item_text.setText("");
		}
		return convertView;
	}

	/** 添加频道列表 */
	public void addItem(Channel channel) {
		channelList.add(channel);
		notifyDataSetChanged();
	}

	/** 拖动变更频道排序 */
	public void exchange(int dragPostion, int dropPostion) {
		holdPosition = dropPostion;
		Channel dragItem = getItem(dragPostion);
		Log.d(TAG, "startPostion=" + dragPostion + ";endPosition="
				+ dropPostion);
		if (dragPostion < dropPostion) {
			channelList.add(dropPostion + 1, dragItem);
			channelList.remove(dragPostion);
			if (dragPostion < mIndex && dropPostion >= mIndex) {
				mIndex--;
			} else if (dragPostion == mIndex) {
				mIndex = dropPostion;
			}
		} else {
			channelList.add(dropPostion, dragItem);
			channelList.remove(dragPostion + 1);
			if (dropPostion <= mIndex && dragPostion > mIndex) {
				mIndex++;
			} else if (dragPostion == mIndex) {
				mIndex = dropPostion;
			}
		}

		isChanged = true;
		notifyDataSetChanged();
	}

	/** 获取频道列表 */
	public List<Channel> getChannnelLst() {
		return channelList;
	}

	/** 设置删除的position */
	public void setRemove(int position) {
		remove_position = position;
		if (position < mIndex) {
			mIndex--;
		} else if (position == mIndex) {
			mIndex = 1;
		}
		notifyDataSetChanged();
	}

	/** 删除频道列表 */
	public void remove() {
		if (remove_position != -1 && channelList.size() > remove_position) {
			channelList.remove(remove_position);
			remove_position = -1;
			notifyDataSetChanged();
		}
	}

	/** 设置频道列表 */
	public void setListDate(List<Channel> list) {
		channelList = list;
	}

	/** 获取是否可见 */
	public boolean isVisible() {
		return isVisible;
	}

	/** 设置是否可见 */
	public void setVisible(boolean visible) {
		isVisible = visible;
	}

	/** 显示放下的ITEM */
	public void setShowDropItem(boolean show) {
		isItemShow = show;
	}

	public boolean isEdit() {
		return isEdit;
	}

	public void setEdit(boolean isEdit) {
		this.isEdit = isEdit;
		this.notifyDataSetChanged();
	}

	public void getUpdate() {
		if (mOnUpdateListener != null) {
			mOnUpdateListener.onUpdateListener();
		}
	}

	public int getIndex() {
		return mIndex;
	}

	public void setIndex(int index) {
		this.mIndex = index;
	}

	public OnUpdateListener getOnUpdateListener() {
		return mOnUpdateListener;
	}

	public void setOnUpdateListener(OnUpdateListener onUpdateListener) {
		this.mOnUpdateListener = onUpdateListener;
	}

	public boolean isSort() {
		return isSort;
	}

	public void setSort(boolean isSort) {
		this.isSort = isSort;
	}

}